package jt.classic.app.fawen;

import jt.classic.system.workflow.WActivity;
import jt.classic.system.workflow.WObject;
import jt.classic.system.workflow.WPlugHandler;
import jt.classic.system.workflow.WorkflowException;

import org.limp.basework.AbstractSimpleBean;
import org.limp.basework.Column;
import org.limp.basework.ColumnNotFoundException;
import org.limp.basework.SimpleBean;
import org.limp.basework.Table;
import org.limp.basework.analyzer.TableRegisterCenter;

import java.sql.Connection;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 发文会签意见聚合插件处理器
 *
 * @author mido
 *
 */
public class WInnerPlugImpl extends AbstractSimpleBean implements WPlugHandler {
    public Object participate(Connection conn, SimpleBean bean)
        throws WorkflowException {
        // TODO Auto-generated method stub
        WActivity act = (WActivity) bean.getResource().get("CURRENTACTIVITY");

        WObject obj = act.instance().iflow().iobject();
        WActivity inner4before = act.instance().inner4before(act.iprocess());

        List parts = new ArrayList();

        Object[] part = new Object[] { "DOC_BJNYNVEHRS", "COL_PYKELUHSGV", "&" };
        
        parts.add(part);

        if ((parts != null) && !parts.isEmpty()) {
            TableRegisterCenter center = TableRegisterCenter.getInstance();

            for (int i = 0; i < parts.size(); i++) {
                Object[] temp = (Object[]) parts.get(i);
                String tableName = (String) temp[0];
                String columnName = (String) temp[1];
                String mark = (String) temp[2];

                Table table = center.findTable(tableName);

                Column column = null;

                try {
                    column = table.column(columnName);
                } catch (ColumnNotFoundException ex) {
                    throw new WorkflowException("workflow inner converge error.",
                        ex);
                }

                List contents = inner4before.icontent(table);

                if (obj.handleTable(table) == 0) {
                    if (!contents.isEmpty()) {
                        StringBuffer result = new StringBuffer();

                        Map info = (Map) contents.get(0);

                        String beforeinfo = (String) info.get(column.getName());

                        result.append(beforeinfo);

                        List outlist = act.instance().inner4out(act.istep()
                                                                   .inner(),
                                act.iprocess());

                        for (int j = 0; j < outlist.size(); j++) {
                            WActivity actout = (WActivity) outlist.get(j);
                            List outcontents = actout.icontent(table);

                            Map outinfo = (Map) outcontents.get(0);

                            String outstr = (String) outinfo.get(column.getName());

                            String cleanstr = parseWantedContent(beforeinfo,
                                    outstr, mark);

                            /*褰撳墠鍐呴儴鐜妭绛剧讲浜嗘剰瑙?*/
                            if (!cleanstr.equals("")) {
                                if (result.toString().equals("")) {
                                    result.append(cleanstr);
                                } else {
                                    result.append(mark + cleanstr);
                                }
                            }
                        }

                        if (bean != null) {
                            /*閲嶇疆鑱氬悎鍐呭*/
                            bean.getResource().put(column.getFormName(),
                                result.toString());
                        } else {
                            /*閲嶇疆鑱氬悎鍐呭*/
                            ((Map) ((List) act.icontent(table)).get(0)).put(column.getName(),
                                result.toString());
                        }
                    }
                } else if (obj.handleTable(table) == 1) {
                }
            }
        }

        return null;
    }

    public Map getResource() {
        // TODO Auto-generated method stub
        return null;
    }

    public void reload(Map map) {
        // TODO Auto-generated method stub
    }

    private String parseWantedContent(String consult, String temp, String mark) {
        StringBuffer result = new StringBuffer();

        String[] cs = consult.split(mark);

        String[] tp = temp.split(mark);

        for (int i = 0; i < tp.length; i++) {
            if (!this.consult(cs, tp[i])) {
                result.append(tp[i]);
            }
        }

        return result.toString();
    }

    private boolean consult(String[] cs, String tp) {
        boolean rnt = false;

        for (int i = 0; i < cs.length; i++) {
            if (!cs[i].equals("") && cs[i].equals(tp)) {
                rnt = true;

                break;
            }
        }

        return rnt;
    }
}
